import { NetService } from '../../common/NetService';
export default {
	data() {
		return {
			id: 0,
			type: 0, //1需要处置，2需要审核
			autoplay: true,
			interval: 2000,
			duration: 500,
			info: {},
			content: "",
			imgArr: [],
			isDis: false,
			typeId: 0, //1 图片 2 视频3附件
			items: [{
				value: "1",
				name: "图片"
			}, {
				value: "2",
				name: "视频"
			}, {
				value: "3",
				name: "附件"
			}],
			userList: [],
		};
	},
	methods: {
		//用户
		wxgetuserbyusername(res) {
			let data = {
				userID: this.type == 1 ? res.reportID : res.mid
			};
			NetService.wxgetuserbyusername(data)
				.then(res => {
					this.userList = res;
					this.selectList = [];
					res.forEach(item => {
						let obj = {
							"label": item.userName,
							"openid": item.openid,
							"value": item.userID,
						}
						this.selectList.push(obj);
					})
					this.cheItem = this.item.user;
					this.item.myType = 3;
					this.itemShow = true;
				})
				.catch(err => {});
		},
		sendMessage(item, status) {
			let openidString = [];
			this.userList.forEach(ite => {
				openidString.push(ite.openid);
			})
			let data = {
				status: status,
				typeId: this.type == 1 ? 2 : 1,
				title: item.title,
				caseId: item.id,
				openidString: openidString.join(","),
			}
			NetService.sendMessage(data)
				.then(res => {})
				.catch(err => {});
		},
		radioChange: function(evt) {
			for(let i = 0; i < this.items.length; i++) {
				if(this.items[i].value == evt.target.value) {
					this.typeId = this.items[i].value;
					break;
				}
			}
		},
		downFile(u) {},
		moreImg(imgList, index) {
			uni.previewImage({
				current: index, //预览图片的下标
				urls: imgList //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
			});
		},
		videoErrorCallback() {
			uni.showToast({
				icon: "none",
				title: "视频播放失败"
			});
		},
		openMap() {
			console.log("地图")
			uni.openLocation({
				latitude: this.info.coordX,
				longitude:  this.info.coordY,
				success: function() {
					console.log('success');
				}
			});
		},
		subMit() {
			if(!this.content) {
				uni.showToast({
					title: "请输入处置内容"
				});
				return false;
			}
			let u = uni.getStorageSync('user');
			let data = {
				dealTypeID: parseInt(this.typeId),
				content: this.content,
				attr: this.imgArr.join(","),
				userID: u.userID,
				recID: this.id,
			}
			this.$set(this, "isDis", true);
			NetService.wxprocessed(data)
				.then(res => {
					uni.showToast({
						title: "处置成功，为您返回上一页"
					});
					this.$set(this, "isDis", false);
					this.sendMessage(this.info, 2);
					setTimeout(function() {
						uni.navigateBack();
					}, 1000);
				})
				.catch(err => {
					this.$set(this, "isDis", false);
				});
		},
		//审核
		shenghe(t) {
			let u = uni.getStorageSync('user');
			let data = {
				checkID: t,
				userID: u.userID,
				recID: this.id,
			}
			this.$set(this, "isDis", true);
			NetService.wxcheck(data)
				.then(res => {
					uni.showToast({
						title: "审核完成，为您返回上一页"
					});
					let lt;
					if(t == 1) {
						lt = 3
					}
					if(t == 2) {
						lt = 4;
					}
					this.$set(this, "isDis", false);
					this.sendMessage(this.info, lt);
					setTimeout(function() {
						uni.navigateBack();
					}, 1000);
				})
				.catch(err => {
					this.$set(this, "isDis", false);
				});
		},
		choseImg() {
			let _this = this;
			let current = this.typeId;
			if(_this.isDis) {
				return false;
			}
			if(current == 1) {
				if(this.imgArr.length >= 9) {
					uni.showToast({
						icon: "none",
						title: "图片只能上传9张"
					});
					return false;
				}

				uni.chooseImage({
					count: 9 - this.imgArr.length,
					success: function(res) {
						_this.$set(_this, "isDis", true);
						_this.upOk(res);
					}
				});
			} else if(current == 2) {
				if(this.imgArr.length >= 1) {
					uni.showToast({
						icon: "none",
						title: "视频只能上传一个"
					});
					return false;
				}
				uni.chooseVideo({
					count: 1,
					sourceType: ['camera', 'album'],
					success: function(res) {
						_this.$set(_this, "isDis", true);
						uni.uploadFile({
							url: '/webApi/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFilePath,
							name: 'file',
							formData: {},
							success: uploadFileRes => {
								_this.imgArr.push(JSON.parse(uploadFileRes.data).data);
							},
							complete: () => {
								_this.$set(_this, "isDis", false);

							}
						});
					}
				});
			} else if(current == 3) {
				if(this.imgArr.length >= 1) {
					if(this.imgArr.length >= 1) {
						uni.showToast({
							icon: "none",
							title: "附件只能上传一个"
						});
						return false;
					}
					return false;
				}
				uni.chooseFile({
					count: 1,
					extension: ['.docx', '.doc', '.doc', '.dotx', '.pdf', '.xls', '.xlsx'],
					success: res => {
						_this.$set(_this, "isDis", true);
						_this.upOk(res);
					}
				});
			}
		},
		upOk(res) {
			let _this = this;
			const tempFilePaths = res.tempFilePaths;
			tempFilePaths.forEach(item => {
				uni.uploadFile({
					url: '/webApi/upload', //仅为示例，非真实的接口地址
					filePath: item,
					name: 'file',
					formData: {},
					success: uploadFileRes => {
						this.imgArr.push(JSON.parse(uploadFileRes.data).data);
						console.log(this.imgArr)
					},
					complete: () => {
						_this.$set(_this, "isDis", false);
					}
				});
			})
		},
		delImg(ind) {
			this.imgArr.splice(ind, 1);
		},
		inputChange(e) {
			let type = e.currentTarget.dataset.i;
			this[type] = e.detail.value;
		},
		init() {
			let _this = this;
			NetService.wxgetrecbyid(this.id)
				.then(res => {
					this.info = res;
					if(this.type) {
						this.wxgetuserbyusername(res);
						//#ifdef H5
						// 判断是否是微信内
						let en = window.navigator.userAgent.toLowerCase();
						// 匹配en中是否含有MicroMessenger字符串
						if(en.match(/MicroMessenger/i) == 'micromessenger') {
							_this.$share({
								title: res.title
							});
						}
						//#endif
					}

				})
				.catch(err => {});
		}
	},
	onLoad(option) {
		let _this = this;
		if(option.id) {
			this.id = parseInt(option.id);
		}
		if(option.type) {
			this.type = parseInt(option.type);
		}
		this.$getOpenId(() => {
			let u = uni.getStorageSync('user');
			if(!u.userID) {
				uni.showToast({
					title: "您未登陆绑定，请登陆"
				});
				setTimeout(function() {
					uni.navigateTo({
						url: "/pages/bind/bind"
					});
				}, 1000);
				return false;
			}
			_this.init();
		});
	}
};